package com.ebupt.migu.music.userGroup.service.impl;

import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.userGroup.entity.UserGroupInfo;
import com.ebupt.migu.music.userGroup.mapper.ProvinceCrowdMapper;
import com.ebupt.migu.music.userGroup.service.ProvinceCrowdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分省运营操作业务端
 *
 * @author lemon
 * @since 2022-09-13 17:51
 */
@Service
@Slf4j
public class ProvinceCrowdServiceImpl implements ProvinceCrowdService {

    @Resource
    private ProvinceCrowdMapper provinceCrowdMapper;

    @Override
    public ResultObject<List<UserGroupInfo>> queryTargetCrowd(UserGroupInfo userGroupInfo) {
        log.info("查询条件：{}", userGroupInfo);
        if (!StringUtils.hasText(userGroupInfo.getPlatformId().toString())) {
            throw new MyException(StatusEnum.BUSINID, "平台标识为空");
        }
        int total;
        List<UserGroupInfo> userGroupInfos;
        try {
            total = provinceCrowdMapper.queryTargetTotal(userGroupInfo);
            userGroupInfos = provinceCrowdMapper.queryTargetCrowd(userGroupInfo);
        } catch (Exception e) {
            log.info("人群查询失败：{}", e.getMessage());
            throw new MyException(StatusEnum.BUSINID, "查询人群失败");
        }
        ResultObject<List<UserGroupInfo>> res = new ResultObject<>();
        res.setCode(StatusEnum.SUCCESS.getCode());
        res.setMsg("人群查询成功，总条数：" + total);
        res.setData(userGroupInfos);
        return res;
    }
}